"""
Case Type   : 问题回归
Case Name   : 备机追数catchup过程中验证主库插入数据
Create At   : 2023/02/06
Owner       : cr13
Description :
    1.开启最大可用模式,设置参数catchup2normal_wait_time=0
    2.建表并插入数据
    3.停止备机
    4.备机停止的前提下主机插入大量数据
    5.step4主机插数据的同时拉起备机
    6.清理环境
Expect      :
    1.成功
    2.生效
    3.成功
    4.成功
    5.成功
    6.成功
History     :
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread
from testcase.utils.Logger import Logger


Primary_SH = CommonSH('PrimaryDbUser')

@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')

class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.commsh_sta = CommonSH('Standby1DbUser')
        self.tb_name = 't_bug_fix_0026'
        self.most_available_sync = self.common.show_param(
            "most_available_sync")
        self.catchup2normal_wait_time = self.common.show_param(
            "catchup2normal_wait_time")

    def test_gs_upgradectl(self):
        text = "step1:开启最大可用模式,设置参数catchup2normal_wait_time=0 " \
               "expect:成功"
        self.log.info(text)
        result = self.commsh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'most_available_sync=on')
        self.assertTrue(result, '执行失败' + text)
        result = self.commsh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'catchup2normal_wait_time=0')
        self.assertTrue(result, '执行失败' + text)

        text = "step2:建表并插入数据 expect:成功"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
            f"create table {self.tb_name}(id int,name text,class " \
            f"varchar(15));insert into {self.tb_name} values(" \
            f"generate_series(1,100),'test','class1');"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败' +
                      text)

        text = "step3:停止备机 expect:成功"
        self.log.info(text)
        result = self.commsh_sta.stop_db_instance()
        self.assertTrue(result, '执行失败' + text)

        text = "step4:备机停止的前提下主机插入大量数据 expect:成功"
        self.log.info(text)
        sql_cmd = f"insert into {self.tb_name} values(" \
            f"generate_series(1,5000000),'test','class1');"
        session1 = ComThread(self.commsh.execut_db_sql, args=(sql_cmd,))
        session1.setDaemon(True)
        session1.start()

        text = "step5:step4主机插数据的同时拉起备机 expect:成功"
        self.log.info(text)
        session2 = ComThread(self.commsh_sta.start_db_instance, args=(
            'standby',))
        session2.setDaemon(True)
        session2.setDaemon(True)
        session2.start()
        session1.join(200)
        session2.join(50)
        result1 = session1.get_result()
        self.log.info(result1)
        result2 = session2.get_result()
        self.log.info(result2)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result1, '执行失败' +
                      text)
        self.assertTrue(result2, '执行失败' + text)

    def tearDown(self):
        text = '-----step6:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        result1 = self.commsh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'most_available_sync={self.most_available_sync}')
        result2 = self.commsh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'catchup2normal_wait_time={self.catchup2normal_wait_time}')
        self.assertTrue(result1, '执行失败' + text)
        self.assertTrue(result2, '执行失败' + text)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
